/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kmcsoft.goldshop.importgold;

import com.kmcsoft.core.ui.GenericController;
import com.kmcsoft.goldshop.generic.service.ShopGenericService;
import com.kmcsoft.goldshop.importgold.service.ImportGoldService;
import com.kmcsoft.goldshop.model.master.Booth;
import com.kmcsoft.goldshop.model.master.GoldSource;
import com.kmcsoft.goldshop.model.master.ProductGold;
import com.kmcsoft.goldshop.model.master.Shop;
import com.kmcsoft.goldshop.model.scm.ImportGoldOrder;
import com.kmcsoft.goldshop.model.scm.ImportGoldDetail;
import com.kmcsoft.util.Utilities;
import com.kmcsoft.utils.i18n;
import com.kmcsoft.zkex.PagingListModel;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.annotation.Autowired;
import org.zkoss.zk.ui.Component;
import org.zkoss.zk.ui.Executions;
import org.zkoss.zk.ui.WrongValueException;
import org.zkoss.zk.ui.event.ForwardEvent;
import org.zkoss.zkplus.databind.BindingListModelList;
import org.zkoss.zul.Combobox;
import org.zkoss.zul.Grid;
import org.zkoss.zul.Row;
import org.zkoss.zul.Window;

/**
 *
 * 
 * @author kmcsoft02
 */
public class ImportGoldComposer extends GenericController<ImportGoldOrder, ImportGoldService> {

    private Collection<Booth> booths;
    private Combobox boothCbx;
    @Autowired
    ShopGenericService shopGenericService;
    private Combobox goldSourceCbx;

    //<editor-fold defaultstate="collapsed" desc="GET">
    public Collection<Booth> getBooths() {
        return booths;
    }

    public Collection<GoldSource> getGoldSources() {
        return service.getGoldSources();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterCompose">
    @Override
    public void doAfterCompose(Component comp) throws Exception {
        super.doAfterCompose(comp);
        booths = shopGenericService.getBooths();
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="PagingListModel">
    @Override
    public PagingListModel getListModel() {
        PagingListModel<ImportGoldOrder> model = Utilities.getPagingListModel(objectClass, HashMap.class, "id, shop.name ,createdDate");
        model.setOrdering("createdDate descending");
        model.initialize();
        return model;
    }

    public void setSelected(HashMap selected) {
        super.setSelected(service.getImportGoldOrderById(selected.get("id")));
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onCreate">
    public void onCreate() {
        onClick$new(null);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doAfterNew">
    @Override
    protected void doAfterNew() {
        super.doAfterNew();
        selected.getDetails().add(new ImportGoldDetail());
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="doBeforeSave">
    @Override
    protected void doBeforeSave() {
        super.doBeforeSave();
        Collection<ImportGoldDetail> checkDetails = new ArrayList<ImportGoldDetail>();
        for (ImportGoldDetail detail : selected.getDetails()) {
            if (detail.getProduct() == null) {
                throw new WrongValueException(i18n.l("shop.importGold.error.product"));
            }
            if (detail.getQuantity() <= 0) {
                throw new WrongValueException(i18n.l("shop.importGold.error.quanlity"));
            }
            for (ImportGoldDetail detail1 : checkDetails) {
                if (detail1.getProduct().equals(detail.getProduct())) {
                    throw new WrongValueException(i18n.l("shop.importGold.error.duplicate.product"));
                }
            }
            checkDetails.add(detail);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onInsertRow">
    public void onInsertRow(ForwardEvent event) {
        Grid grid = (Grid) event.getOrigin().getTarget().getParent().getParent().getParent();
        selected.getDetails().add(new ImportGoldDetail());
        getBinder().loadComponent(grid);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onRemove">
    public void onRemove(ForwardEvent event) {
        Grid grid = (Grid) event.getOrigin().getTarget().getParent().getParent().getParent();
        if (grid.getModel().getSize() == 1) {
            return;
        }
        Row row = ((Row) event.getOrigin().getTarget().getParent());
        Object selectedRecord = row.getValue();
        ((BindingListModelList) grid.getModel()).remove(selectedRecord);
        grid.renderAll();
    }
    //</editor-fold> 

    //<editor-fold defaultstate="collapsed" desc="onLoadBooth">
    public void onLoadBooth(ForwardEvent event) {
        Shop shop = (Shop) ((Combobox) event.getOrigin().getTarget()).getSelectedItem().getValue();
        booths = shop.getBooths();
        getBinder().loadComponent(boothCbx);
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onAddGoldSource">
    public void onAddGoldSource() {
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("binder", getBinder());
            map.put("component", goldSourceCbx);
            Window win = (Window) Executions.createComponents("/shop/modal/goldSourceModal.zul", null, map);
            win.doModal();
        } catch (Exception ex) {
            log.error("ERROR from onAddGoldSource", ex);
        }
    }
    //</editor-fold>

    //<editor-fold defaultstate="collapsed" desc="onLoadUnit">
    public void onLoadUnit(ForwardEvent event) {
        Combobox combobox = (Combobox) event.getOrigin().getTarget();
        ProductGold gold = (ProductGold) combobox.getSelectedItem().getValue();
        ((ImportGoldDetail) ((Row) combobox.getParent()).getValue()).setUnit(gold.getUnit());
        getBinder().loadComponent(combobox.getNextSibling());
    }
    //</editor-fold>
    
    
}
